package com.pentalog.common.bo.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pentalog.common.bo.MultiDBService;
import com.pentalog.stock.dao.StockDao;
import com.pentalog.stock.dao2.AppUserDao;
import com.pentalog.stock.model.AppUser;

@Transactional
@Service("multiDBService")
public class MultiDBServiceImpl implements MultiDBService {

	@Autowired
	StockDao stockDao;

	@Autowired
	AppUserDao appUserDao;

	public void doSomeThingWithMultiDB() {

		stockDao.findByStockCode("123");

		AppUser user;

		user = appUserDao.findByUserName("tnguyenmanh");
		if (user == null) {

			user = new AppUser(false, false, "Hanoi", "1234", false,
					"tnguyenmanh@pentalog.fr", "Tuong", "Nguyen Manh",
					"ohlala", "tnguyenmanh");
			appUserDao.save(user);
		}

		user = appUserDao.findByUserName("ttranhuu");
		if (user == null) {

			user = new AppUser(false, false, "Hanoi", "1234", false,
					"ttranhuu@pentalog.fr", "Thuy", "Tran Huu", "ohlala",
					"ttranhuu");
			appUserDao.save(user);
		}

		user = appUserDao.findByUserName("qbuingoc");
		if (user == null) {

			user = new AppUser(false, false, "Hanoi", "1234", false,
					"qbuingoc@pentalog.fr", "Quang", "Bui Ngoc", "ohlala",
					"qbuingoc");
			appUserDao.save(user);
		}

		user = appUserDao.findByUserName("hhoangvan");
		if (user == null) {
			user = new AppUser(false, false, "Hanoi", "1234", false,
					"hhoangvan@pentalog.fr", "Huan", "Hoang Van", "ohlala",
					"hhoangvan");
			appUserDao.save(user);
		}

		user = appUserDao.findByUserName("hdovan");
		if (user == null) {
			user = new AppUser(false, false, "Hanoi", "1234", false,
					"hdovan@pentalog.fr", "Hanh", "Do Van", "ohlala", "hdovan");

			appUserDao.save(user);
		}

		List<AppUser> usersList = appUserDao.getAllAppUsers();
		System.out.println("------------------------------------------------");
		for (AppUser appUser : usersList) {
			System.out.println("User name : " + appUser.getUsername());
			System.out.println("Password : " + appUser.getPassword());
			System.out.println("Address : " + appUser.getAddress());
			System.out
					.println("------------------------------------------------");
		}

	}

}
